package com.easidea.tim.flex.remote.customer
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.SysLocator;
	import com.easidea.tim.flex.bean.customer.Customer;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;
	
	
	
	public class CustomerDao extends BaseDao
	{
		private var _customerService:HibernateRemoteObject = new HibernateRemoteObject();
		private var _customerId:int;
		private static var _instance:CustomerDao;
		
		public static function getInstance():CustomerDao{
			if( !_instance ){
				_instance = new CustomerDao();
				
			}
			return _instance;
		}
		public function CustomerDao() {
				_customerService.channelSet = this.buildChannelSet();
				_customerService.destination="customerService";	
				_customerService.concurrency = "last";
				_customerService.makeObjectsBindable = true;
				_customerService.showBusyCursor = true;
		}

		/**
		 * 调用spring方法:根据客户名称等参数分页查询客户列表
		 * @param name
		 * @param page
		 * 
		 */		
		public function getCustomerList(name:String,areaId:int,contactor:String, salesName:String, page:Page):void {			
			_customerService.addEventListener(ResultEvent.RESULT, showCustomerList);
			_customerService.addEventListener(FaultEvent.FAULT, _Fault);			
			_customerService.getCustomerListByCondition(name,areaId,contactor,salesName,page,SysLocator.getInstance().loggedUserId);
		}
		/**
		 * 根据客户名称等参数分页查询客户列表回调函数
		 * @param e
		 * 
		 */			
		private function showCustomerList(e:ResultEvent):void {			 
			_customerService.removeEventListener(ResultEvent.RESULT, showCustomerList);

			 if(e.result != null) {		
				var page:Page = e.result as Page;					 				 
				Env.dispatchEvent(new CustomerEvent("getCustomerListSuccess",page));				  									 
			 } 
		}


		/**
		 *调用spring方法:新增或编辑客户保存 
		 * @param customer
		 * 
		 */		
		public function saveCustomer(customer:Customer):void {
				_customerService.addEventListener(FaultEvent.FAULT, _Fault);
				_customerService.addEventListener(ResultEvent.RESULT, saveCustomerSuccess);
				_customerService.saveOrUpdateCustomer(customer);
		}
	   /**
	    *新增或编辑客户保存回调函数 
	    * @param e
	    * 
	    */		
	   private function saveCustomerSuccess(e:ResultEvent):void {			 
			_customerService.removeEventListener(ResultEvent.RESULT, saveCustomerSuccess);
			 if(e.result != null && e.result == "success") {
				AppAlert.show("保存成功！","提示");
				Env.dispatchEvent(new CustomerEvent("saveCustomerSuccess"));
			 } else if(e.result == "ExistSameName"){
			 	//AppAlert.show("存在相同的客户类别名称，请重新输入！","提示");
			 	Env.dispatchEvent(new CustomerEvent("saveCustomerFail",e.result));
			 }else {
			 	//AppAlert.show(e.result as String,"提示");
			 	trace(e.message);
			 	Env.dispatchEvent(new CustomerEvent("saveCustomerFail",e.result));
			 }		
		}
		/**
		 *调用spring方法:删除客户
		 * @param customer
		 * 
		 */		
		public function deleteCustomer(id:String):void {
				_customerService.addEventListener(FaultEvent.FAULT, _Fault);
				_customerService.addEventListener(ResultEvent.RESULT, deleteCustomerSuccess);
				_customerService.deleteCustomer(id);
		}
	   /**
	    * 删除客户回调函数 
	    * @param e
	    * 
	    */		
	   private function deleteCustomerSuccess(e:ResultEvent):void {			 
			_customerService.removeEventListener(ResultEvent.RESULT, deleteCustomerSuccess);
			 if(e.result) {
				AppAlert.show("删除成功！","提示");
				Env.dispatchEvent(new CustomerEvent("deleteCustomerSuccess"));
			 }else{
			 	AppAlert.show("删除失败： 该客户有未完成订单或数据错误！","提示");
			 	Env.dispatchEvent(new CustomerEvent("deleteCustomerSuccess")); //仅作数据刷新使用
			 }
		}
		
		
		
		/**
		 *  获得应收款总额
		 *  @author Harrison
		 */
		public function getCustomerDuefromSummary():void{
			_customerService.addEventListener(FaultEvent.FAULT, _Fault);
			_customerService.addEventListener(ResultEvent.RESULT, getCustomerDuefromSummaryResult);
			_customerService.getCustomerDuefromSummary();
		}
		
		private function getCustomerDuefromSummaryResult(e:ResultEvent):void{
			_customerService.removeEventListener(ResultEvent.RESULT, getCustomerDuefromSummaryResult);
			Env.dispatchEvent(new CustomerEvent("getCustomerDuefromSummarySuccess",e.result));
		}
		
		/**
		 *  列举客户的应收款总额
		 *  @param 客户名称
		 *  @param 销售人员名称 
		 *  @author Harrison
		 */
		public function listCustomerDueFromSummary(name:String,salesName:String, page:Page):void{
			_customerService.addEventListener(FaultEvent.FAULT, _Fault);
			_customerService.addEventListener(ResultEvent.RESULT, listCustomerDueFromSummaryResult);
			_customerService.listCustomerDueFromSummary(name,salesName,page);
		}
		
		private function listCustomerDueFromSummaryResult(e:ResultEvent):void{
			_customerService.removeEventListener(ResultEvent.RESULT,listCustomerDueFromSummaryResult);
			Env.dispatchEvent(new CustomerEvent("listCustomerDueFromSuccess",e.result));
		}
		
	
		/**
		 * 保存对账信息
		 * @param id 客户ID
		 * @param memo 信息
		 * @author Harrison
		 */
		public function saveAccountMemo(id:int,memo:String):void{
			_customerService.addEventListener(FaultEvent.FAULT, _Fault);
			_customerService.addEventListener(ResultEvent.RESULT, saveAccountMemoResult);
			_customerService.saveAccountMemo(id,memo);
		}
		private function saveAccountMemoResult(e:ResultEvent):void{
			_customerService.removeEventListener(ResultEvent.RESULT,saveAccountMemoResult);
			Env.dispatchEvent(new CustomerEvent("saveAccountMemoSuccess",e.result));
		}
		
		
		/**
		 * 根据客户名称过滤
		 * @param 客户名称
		 * @author Harrison
		 * 
		 */
		public function filterCustomerByName(name:String):void{
			_customerService.addEventListener(ResultEvent.RESULT,filterCustomerByNameResult);
			_customerService.addEventListener(FaultEvent.FAULT,_Fault);
			_customerService.getCustomerListByName(name);
		}
		
		private function filterCustomerByNameResult(e:ResultEvent):void{
			_customerService.removeEventListener(ResultEvent.RESULT,filterCustomerByNameResult);
			Env.dispatchEvent(new CustomerEvent("filterCustomerByNameSuccess",e.result));
		} 
	}
}